/**
 * @api post model.
 *
 * User: yunshengzhu
 * Date: 2020/12/10
 * Time: 上午10:40
 */
package model

import "pkg/cfg"

func CreateUserWithdraw(obj UserWithdrawDb) error {
	tx := db.Begin()
	err := tx.Create(&obj).Error
	if err != nil {
		cfg.LogErr("err:", err)
		tx.Rollback()
		return err
	}
	err = tx.Exec(`update t_user set amount=amount-? where user_id=?`, obj.Amount+obj.ProcedureAmount, obj.UserId).Error
	if err != nil {
		cfg.LogErr("err:", err)
		tx.Rollback()
		return err
	}
	tx.Commit()
	return nil
}

func GetUserWithdrawList(userId int64, page, size int) (list []UserWithdrawDb, err error) {
	list = make([]UserWithdrawDb, 0)
	return list, db.Where("user_id=?", userId).Offset((page - 1) * size).Limit(size).Find(&list).Error
}

func GetUserWithdrawCount(userId int64) (num int64, err error) {
	count := UserWithdrawCountDb{}
	return count.Count, db.Where("user_id=?", userId).Select("count(*) as count").Take(&count).Error
}

func UpdateUserWithdraw(obj UserWithdrawDb) error {
	return db.Where("order_number=?", obj.OrderNumber).Select("status,remark,check_time").Updates(obj).Error
}
